/**
 * 页面框架
 */
(function ($, owner) {
    /**
     * 初始化自动绑定
     */
    owner.init = function () {
        //处理绑定组件
        $("[data-dux]").each(function () {
            var data = $(this).data(), name = data['dux'], names = name.split('-', 2);
            window[names[0]][names[1]](this, data);
        });

    };
}(jQuery, window.dux = {}));



(function ($, owner) {
    /**
     * 消息提示
     * @param msg
     */
    owner.msg = function (msg) {
        Do('dialog', function () {
            if (mobile) {
                layer.open({
                    content: msg
                    , skin: 'msg'
                    , time: 3
                });
            } else {
                layer.msg(msg);
            }
        });
    };
    /**
     * AJAX确认
     * @param $el
     */
    owner.ajaxConfirm = function ($el) {
        $($el).click(function () {
            var data = $(this).data();
            Do('dialog_amazeui', function () {
                amDialog.confirm({
                    title: '确认',
                    content: data.title,
                    onConfirm: function() {
                        app.ajax({
                            url: data.url,
                            type: 'post',
                            data: data.params,
                            success: function (msg, url) {
                                var callback = data.callback;
                                if (callback != undefined && callback != '') {
                                    window[callback](msg, url);
                                } else {
                                    location.reload();
                                    layer.close(index);
                                }
                            },
                            error: function (msg) {
                                app.error(msg);
                                layer.close(index);
                            }
                        });
                    },
                    onCancel: function() {
                    }
                });
            });
        });
    };

    /**
     * 询问窗口
     * @param config
     */
    owner.confirm = function (config) {
        var defaultConfig = {
            success: {},
            error: {}
        };
        config = $.extend(defaultConfig, config);
        Do('dialog_amazeui', function () {
            amDialog.confirm({
                title: config.title,
                btn : config.btn,
                callback : config.callback
            });
        });
    };

    /**
     * 打开窗口
     * @param $el
     * @param config
     */
    owner.open = function ($el, config) {
        var defaultConfig = {
            width: '500px',
            height: '400px'
        };
        config = $.extend(defaultConfig, config);

        Do('dialog_amazeui', function () {
            $($el).on('click', function () {
                amDialog.open({
                    title: config.title,
                    url: config.url,
                    width : config.width,
                    height : config.height
                });
            });
        });
    };

    /**
     * 关闭窗口
     */
    owner.close = function () {
        amDialogIframe.modal('close');
    };

    /**
     * 确认对话框
     * @param $el
     * @param config
     */
    owner.alert = function (config) {
        Do('dialog_amazeui', function () {
            amDialog.alert({
                title: config.title,
                onConfirm: function() {
                    if (typeof config.callback == 'function') {
                        config.callback();
                    }
                }
            });
        });
    };

}(jQuery, window.dialog = {}));

/**
 * 常用方法
 */
(function ($, owner) {
    /**
     * 调试方法
     * @param msg
     */
    owner.debug = function (msg) {
        if (typeof(console) != 'undefined') {
            console.log(msg);
        }
    };
    /**
     * AJAX请求
     * @param config
     */
    owner.ajax = function (config) {
        var defaultConfig = {};
        config = $.extend(defaultConfig, config);
        $.ajax({
            url: config.url,
            type: config.type,
            data: config.data,
            dataType: 'json',
            success: function (json) {
                if (typeof config.success == 'function') {
                    config.success(json.message, json.url);
                }
            },
            error: function (e) {
                try {
                    var json = eval('(' + e.responseText + ')');
                    var msg = json.message;
                } catch (e) {
                    var msg = null;
                }
                if (msg == '' || msg == null) {
                    msg = '数据请求失败，请刷新后再试！';
                }

                if (e.status == 404) {
                    app.error('请求操作不存在!');
                    return;
                }

                if(config.loginJump) {
                    if (e.status == 401) {
                        if (typeof config.login == 'function') {
                            config.login(msg);
                            return;
                        } else {
                            app.error(msg, json.url);
                            return;
                        }
                    }
                }


                if (typeof config.error == 'function') {
                    config.error(msg);
                }
            }
        });
    };
    /**
     * 成功提示
     * @param msg
     * @param url
     */
    owner.success = function (msg, url) {
        Do('dialog', function () {
            if (url) {
                window.location.href = url;
            } else {
                dialog.msg(msg);
            }
        });
    };
    /**
     * 失败提示
     * @param msg
     * @param url
     */
    owner.error = function (msg, url) {

        Do('dialog', function () {
            if (url) {
                layer.open({
                    content: msg,
                    btn: ['确定'],
                    yes: function (index) {
                        if (url) {
                            window.location.href = url;
                        } else {
                            layer.close(index);
                        }
                    }
                });
            } else {
                dialog.msg(msg);
                return false;
            }
        });
    };

    /**
     * 移动端检测
     * @returns {boolean}
     */
    owner.mobile = function () {
        var check = false;
        if(navigator.userAgent.match(/(mobile|iPhone|iPod|Android|ios)/i)){
            check = true;
        }else if (screen.width <= 1024){
            check = true;
        }
        return check;
    };

    /**
     * 复制内容
     * @param $el
     */
    owner.copy = function ($el) {
        Do('copy','dialog', function () {
            var client = new ZeroClipboard($el);
            client.on("aftercopy", function (event) {
                dialog.msg('复制成功，请直接粘贴使用！');
            });

        });

    }

}(jQuery, window.app = {}));